package mall.controller.client;

import mall.core.session.SessionKey;
import mall.core.web.Controller;
import mall.entity.Cart;
import mall.entity.User;
import mall.service.CartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/client/cart")
public class ClientCartController extends Controller {

    private final CartService cartService = new CartService();


    /**
     * 切换选中
     */
    private void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String selected1 = req.getParameter("selected");
        int selected = Integer.parseInt(selected1);
        Integer cartId = Integer.valueOf(req.getParameter("cartId"));
        cartService.selected(selected == 2 ? 0 : 1, cartId);

        redirect("/client/cart?method=index",req,resp);
    }


    /**
     * 前台购物车页面
     */
    public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User loginUser = (User) req.getSession().getAttribute(SessionKey.LOGIN_USER);
        List<Cart> cartList = cartService.list(loginUser.getId());

        BigDecimal totalPrice = BigDecimal.ZERO;
        Integer selectedQuantity = 0;
        if (cartList != null) {
            for (Cart cart : cartList) {
                if (cart.getSelected() == 1) {
                    BigDecimal itemPrice = cart.getProduct().getPrice();
                    BigDecimal quantity = new BigDecimal(cart.getQuantity());
                    totalPrice = totalPrice.add(itemPrice.multiply(quantity));
                    selectedQuantity += 1;
                }
            }
        }


        req.setAttribute("name", loginUser.getName());
        req.setAttribute("phone", loginUser.getPhone());
        req.setAttribute("address", loginUser.getAddress());

        req.setAttribute("cartList", cartList);
        req.setAttribute("selectedQuantity", selectedQuantity);
        req.setAttribute("totalPrice", totalPrice);
        forward("/client/cart", req, resp);
    }


    /**
     * 新增购物车
     */
    public void create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer productId = Integer.valueOf(req.getParameter("productId"));
        User loginUser = (User) req.getSession().getAttribute(SessionKey.LOGIN_USER);
        boolean exist = cartService.exist(loginUser.getId(), productId);
        if (!exist) {
            Cart cart = new Cart();
            cart.setProductId(productId);
            cart.setUserId(loginUser.getId());
            cart.setQuantity(1);
            cartService.save(cart);
        }


        redirect("/client/cart?method=index",req,resp);
    }


    /**
     * 删除购物车
     */
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer cartId = Integer.valueOf(req.getParameter("cartId"));
        cartService.remove(cartId);

        redirect("/client/cart?method=index",req,resp);
    }


    /**
     * 修改购物车数量
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer cartId = Integer.valueOf(req.getParameter("cartId"));
        Integer quantity = Integer.valueOf(req.getParameter("quantity"));

        Cart cart = new Cart();
        cart.setId(cartId);
        cart.setQuantity(quantity);
        cartService.update(cart);

        redirect("/client/cart?method=index",req,resp);
    }
}
